package com.example.fresh_market.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.fresh_market.R;
import com.example.fresh_market.entity.Address;

import java.util.ArrayList;
import java.util.List;

public class AddressAdapter extends RecyclerView.Adapter<AddressAdapter.ViewHolder> {

    private final ArrayList<Address> mList;
    private final OnItemClickListener mListener;

    public AddressAdapter(ArrayList<Address> list, OnItemClickListener listener) {
        this.mList = list;
        this.mListener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.address_item, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Address address = getItem(position);
        holder.name.setText(address.getRealName());
        holder.phone.setText(address.getPhone());
        holder.detail.setText(String.format("%s-%s-%s-%s", address.getProvince(), address.getCity(), address.getDistrict(), address.getDetail()));
        holder.isDefault.setChecked(address.getIsDefault());
        if (address.getIsDefault()) {
            holder.isDefault.setText("已默认");
        } else {
            holder.isDefault.setText("设为默认");
        }
        holder.delete.setOnClickListener(v -> {
            mListener.onDeleteClick(v, holder.getAdapterPosition());
        });
        holder.edit.setOnClickListener(v -> {
            mListener.onEditClick(v, holder.getAdapterPosition());
        });
        holder.isDefault.setOnClickListener(v -> {
            mListener.onDefaultClick(v, holder.getAdapterPosition());
        });
        holder.itemLayout.setOnClickListener(v -> {
            mListener.onItemClick(v, holder.getAdapterPosition());
        });
    }

    public Address getItem(int position) {
        return mList.get(position);
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public void setData(List<Address> list) {
        mList.clear();
        mList.addAll(list);
        notifyDataSetChanged();
    }

    public void removeData(int position) {
        mList.remove(position);
        notifyItemRemoved(position);
    }

    public interface OnItemClickListener {
        void onDeleteClick(View v, int position);

        void onEditClick(View v, int position);

        void onDefaultClick(View v, int position);

        void onItemClick(View v, int position);
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private LinearLayout itemLayout;
        private TextView name;
        private TextView phone;
        private TextView detail;
        private CheckBox isDefault;
        private TextView delete;
        private TextView edit;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            itemLayout = itemView.findViewById(R.id.address_item_layout);
            name = itemView.findViewById(R.id.address_name);
            phone = itemView.findViewById(R.id.address_phone);
            detail = itemView.findViewById(R.id.address_detail);
            isDefault = itemView.findViewById(R.id.address_default);
            delete = itemView.findViewById(R.id.address_delete);
            edit = itemView.findViewById(R.id.address_edit);
        }
    }
}
